﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using AudioPlayer.Entity;
using System.Collections;
namespace AudioPlayer.GUI.Music {
	/// <summary>
	/// Music Node
	/// </summary>
	internal class MusicNode {
		#region Fields
		private String _text;
		private Rectangle _bounds;
		private Bitmap _imageSelect;
		private Bitmap _imageNormal;

		private bool _selected;
		private bool _editable;

		private Object _tag;
		private MusicGroup _group;
		#endregion
		#region Constructor
		/// <summary>
		/// Initializes a new instance of the <see cref="MusicNode"/> class.
		/// </summary>
		/// <param name="text">Node Text.</param>
		public MusicNode(string text) : this(text,false){

		}
		/// <summary>
		/// Initializes a new instance of the <see cref="MusicNode"/> class.
		/// </summary>
		/// <param name="text">Node Text.</param>
		/// <param name="editable">if set to <c>true</c> means node is editable.</param>
		public MusicNode(string text,bool editable) : this(text,editable,null,null) {
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="MusicNode"/> class.
		/// </summary>
		/// <param name="text">The Text.</param>
		/// <param name="editable">if set to <c>true</c> means node is editable.</param>
		/// <param name="imageNormal">Normal Image.</param>
		/// <param name="imageSelect">Selected Image.</param>
		public MusicNode(string text, bool editable,Bitmap imageNormal, Bitmap imageSelect) {
			this.Text = text;
			this.Editable = editable;
			this.ImageNormal = imageNormal;
			this.ImageSelect = imageSelect;
		}
		#endregion
		#region Private Voids

		#endregion
		#region Protected
		/// <summary>
		/// Raises the <see cref="E:MouseClick"/> event.
		/// </summary>
		/// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
		protected virtual void OnMouseClick(MouseEventArgs e) {
			//Check if it's not null
			if (this.MouseClick != null) {
				this.MouseClick(this, e);
			}
		}
		#endregion
		#region Public Voids
		/// <summary>
		/// Clicks the node.
		/// </summary>
		/// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
		public void Click(MouseEventArgs e) {
			this.OnMouseClick(e);
		}
		#endregion
		#region Property
		public String Text {
			get { return _text; }
			set { _text=value; }
		}

		public Rectangle Bounds {
			get { return _bounds; }
			set { _bounds=value; }
		}
		public Bitmap ImageSelect {
			get { return _imageSelect; }
			set { _imageSelect=value; }
		}
		public Bitmap ImageNormal {
			get { return _imageNormal; }
			set { _imageNormal=value; }
		}
		public Object Tag {
			get { return _tag; }
			set { _tag = value; }
		}
		public MusicGroup Group {
			get { return _group; }
			set { _group = value; }
		}

		
		public bool Selected {
			get { return _selected; }
			set { _selected = value; }
		}
		public bool Editable {
			get { return _editable; }
			set { _editable = value; }
		}
		public bool Visible {
			get {
				if (_group != null) {
					if (_group.Expanded) {
						return true;
					} else {
						return false;
					}
				}
				return false;
			}
		}
		#endregion
		#region Events
		/// <summary>
		/// Occurs when node is clicked.
		/// </summary>
		public event MouseEventHandler MouseClick;
		#endregion
	}
	/// <summary>
	/// Contains nodes.
	/// </summary>
	internal class MusicNodeCollection : IList<MusicNode> {
		#region Fields
		private MusicGroup _owner;
		private List<MusicNode> _list;
		#endregion
		#region Constructor
		public MusicNodeCollection(MusicGroup owner) {
			_owner = owner;
			_list = new List<MusicNode>();
		}
		#endregion
		#region Public Voids

		/// <summary>
		/// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"/>.
		/// </summary>
		/// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"/>.</param>
		/// <returns>
		/// The index of <paramref name="item"/> if found in the list; otherwise, -1.
		/// </returns>
		public int IndexOf(MusicNode item) {
			return _list.IndexOf(item);
		}

		/// <summary>
		/// Inserts an item to the <see cref="T:System.Collections.Generic.IList`1"/> at the specified index.
		/// </summary>
		/// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
		/// <param name="item">The object to insert into the <see cref="T:System.Collections.Generic.IList`1"/>.</param>
		/// <exception cref="T:System.ArgumentOutOfRangeException">
		/// 	<paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.</exception>
		/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.</exception>
		public void Insert(int index, MusicNode item) {
			_list.Insert(index, item);
		}

		/// <summary>
		/// Removes the <see cref="T:System.Collections.Generic.IList`1"/> item at the specified index.
		/// </summary>
		/// <param name="index">The zero-based index of the item to remove.</param>
		/// <exception cref="T:System.ArgumentOutOfRangeException">
		/// 	<paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.</exception>
		/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.</exception>
		public void RemoveAt(int index) {
			_list.RemoveAt(index);
		}

		/// <summary>
		/// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
		/// </summary>
		/// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
		/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
		public void Add(MusicNode item) {
			_list.Add(item);
			item.Group = _owner;
		}

		/// <summary>
		/// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
		/// </summary>
		/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception>
		public void Clear() {
			_list.Clear();
		}

		/// <summary>
		/// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
		/// </summary>
		/// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
		/// <returns>
		/// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
		/// </returns>
		public bool Contains(MusicNode item) {
			return _list.Contains(item);
		}

		/// <summary>
		/// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
		/// </summary>
		/// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param>
		/// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
		/// <exception cref="T:System.ArgumentNullException">
		/// 	<paramref name="array"/> is null.</exception>
		/// <exception cref="T:System.ArgumentOutOfRangeException">
		/// 	<paramref name="arrayIndex"/> is less than 0.</exception>
		/// <exception cref="T:System.ArgumentException">
		/// 	<paramref name="array"/> is multidimensional.-or-<paramref name="arrayIndex"/> is equal to or greater than the length of <paramref name="array"/>.-or-The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.-or-Type <paramref name="T"/> cannot be cast automatically to the type of the destination <paramref name="array"/>.</exception>
		public void CopyTo(MusicNode[] array, int arrayIndex) {
			_list.CopyTo(array, arrayIndex);
		}
		/// <summary>
		/// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
		/// </summary>
		/// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
		/// <returns>
		/// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
		/// </returns>
		/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
		public bool Remove(MusicNode item) {
			bool ret = _list.Remove(item);
			if (ret) {
				item.Group = null;
			}
			return ret;
		}
		#endregion
		#region Property
		/// <summary>
		/// Gets or sets the <see cref="AudioPlayer.GUI.Music.MusicNode"/> at the specified index.
		/// </summary>
		/// <value></value>
		public MusicNode this[int index] {
			get {
				return _list[index];
			}
			set {
				_list[index] = value;
			}
		}
		/// <summary>
		/// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
		/// </summary>
		/// <value></value>
		/// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</returns>
		public int Count {
			get { return _list.Count; }
		}

		/// <summary>
		/// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
		/// </summary>
		/// <value></value>
		/// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only; otherwise, false.</returns>
		public bool IsReadOnly {
			get { return false; }
		}
		#endregion
		#region IEnumerable Members

		/// <summary>
		/// Returns an enumerator that iterates through a collection.
		/// </summary>
		/// <returns>
		/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
		/// </returns>
		IEnumerator IEnumerable.GetEnumerator() {
			return _list.GetEnumerator();
		}
		/// <summary>
		/// Returns an enumerator that iterates through the collection.
		/// </summary>
		/// <returns>
		/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
		/// </returns>
		public IEnumerator<MusicNode> GetEnumerator() {
			return _list.GetEnumerator();
		}
		#endregion
	}
}
